﻿using System.Collections.Generic;
using System.Web;
using ZQNB.BaseLib.Core;
using ZQNB.BaseLib.Core.Models;

namespace ZQNB.Web.Common.Helpers
{
    internal interface ISuperAccountHelper
    {
        /// <summary>
        /// 检测当前的用户是否合法
        /// </summary>
        /// <param name="httpRequest">当前请求上下文</param>
        /// <returns></returns>
        bool CheckIdentity(HttpRequestBase httpRequest);
    }

    public class SuperAccountHelper : ISuperAccountHelper
    {
        private readonly ISuperAccountService _superAccountService;

        public SuperAccountHelper(ISuperAccountService superAccountService)
        {
            _superAccountService = superAccountService;
        }

        public bool CheckIdentity(HttpRequestBase httpRequest)
        {
            const string super = "super";
            var token = httpRequest.Params[super];
            if (string.IsNullOrWhiteSpace(token))
            {
                return false;
            }

            IList<string> superAccounts = _superAccountService.GetSuperAccounts();
            if (!superAccounts.Contains(super))
            {
                return false;
            }

            SuperAccount superAccount = new SuperAccount {Username = super, Password = token};
            return _superAccountService.ValidatePassword(superAccount);
        }
    }
}